<template>
  <div class="operation">
    <el-row>
      <el-button icon="el-icon-plus" @click="adddialogVisible = true"
        >添加</el-button
      >
      <el-button icon="el-icon-delete" @click="dele">删除</el-button>
    </el-row>
    <el-table
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      @selection-change="handleSelectionChange"
      height="calc( 100vh - 200px)"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="cahnge(scope.row)"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="handleClickq(scope.row)"
            >权限</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="编号" width="120">
        <template slot-scope="scope">{{ scope.row.Id }}</template>
      </el-table-column>
      <el-table-column
        prop="OperatorCode"
        label="编码"
        show-overflow-tooltip
        width="90"
      />
      <el-table-column
        prop="OperatorName"
        label="名称"
        show-overflow-tooltip
        width="90"
      />
      <!-- <el-table-column
        prop="jurisdiction"
        label="权限组"
        show-overflow-tooltip
        width="130"
      /> -->
      <el-table-column
        prop="rolename"
        label="角色"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        prop="LastLogin"
        label="最后登陆时间"
        width="190"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{
          formatDate(scope.row.LastLogin)
        }}</template>
      </el-table-column>
      <el-table-column
        prop="UsingFlag"
        label="使用标志"
        show-overflow-tooltip
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.UsingFlag == 1 ? "使用" : "停用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="Note"
        label="备注"
        show-overflow-tooltip
        width="190"
      />
    </el-table>
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="clear"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <el-form-item label="编码">
          <el-input v-model="inputcode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="inputname" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="角色编码">
          <el-select v-model="adcode" placeholder="请选择">
            <el-option
              v-for="item in addicode"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="最后登录时间">
          <el-input
            v-model="inputtime"
            placeholder="请输入最后登录时间"
            disabled
          />
        </el-form-item>
        <el-form-item label="使用标志">
          <el-select v-model="inputsign" placeholder="请输入使用标志">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="inputremarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changepw()">重置密码</el-button>
        <el-button type="primary" @click="handleClose()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增模态框 -->
    <el-dialog
      title="新增"
      :visible.sync="adddialogVisible"
      width="30%"
      :before-close="clear"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <el-form-item label="操作员编码">
          <el-input v-model="addinputcode" placeholder="请输入操作员编码" />
        </el-form-item>
        <el-form-item label="操作员姓名">
          <el-input v-model="addinputname" placeholder="请输入操作员姓名" />
        </el-form-item>
        <el-form-item label="角色编码">
          <el-select v-model="adcode" placeholder="请选择">
            <el-option
              v-for="item in addicode"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="addpw" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="验证密码">
          <el-input type="password" v-model="addpwo" placeholder="请输入密码" />
        </el-form-item>
        <!-- <el-form-item label="最后登录时间">
          <el-input
            v-model="addinputtime"
            placeholder="请输入最后登录时间"
            disabled
          />
        </el-form-item> -->
        <el-form-item label="使用标志">
          <el-select v-model="addinputsign" placeholder="请输入使用标志">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addinputremarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCloseadd()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 权限 -->
    <el-dialog
      title="权限修改"
      :visible.sync="dialogVisibleq"
      width="30%"
      :before-close="clear"
    >
      <!-- 权限管理控制 -->
      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        ref="tree"
        :default-expanded-keys="[]"
        :default-checked-keys="arrey"
        :props="defaultProps"
        @node-click="handleNodeClick"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear">取 消</el-button>
        <el-button type="primary" @click="handleCloseq()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  API_GET_OPERATOR_LIST,
  API_OPERATOR_NEW,
  API_OPERATOR_XG,
  API_OPERATOR_SC,
  API_GET_USER_LIST,
  API_OPERATOR_GETCD,
  API_OPERATOR_SZQX,
  API_OPERATOR_CZMM,
  API_VILL_HQQX,
} from "../../api/system";
export default {
  name: "",
  data() {
    return {
      msg: "操作管理",
      labelPosition: "right",
      tableData: [],
      multipleSelection: [],
      dialogVisible: false, // 修改模块显示隐藏
      adddialogVisible: false, // 新增模块显示隐藏
      /* 可更改内容 */
      inputcode: "",
      inputname: "",
      inputjurisdiction: "",
      inputtime: "",
      inputsign: "",
      inputremarks: "",
      /* 新增内容 */
      addinputcode: "",
      addinputname: "",
      addinputjurisdiction: "",
      addinputtime: "",
      addinputsign: "",
      addinputremarks: "",
      options: [
        {
          value: "1",
          label: "使用",
        },
        {
          value: "2",
          label: "停用",
        },
      ],
      adcode: "",
      addicode: [],
      addpw: "",
      addpwo: "",
      Id: "",
      data: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      dialogVisibleq: false,
      ary: [],
      arrey: [],
    };
  },
  methods: {
    // 重置密码
    changepw() {
      let data = { operid: this.Id };
      API_OPERATOR_CZMM(data)
        .then((res) => {
          console.log(res);
          this.$message.success("密码重置成功！初始密码：123456");
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // MD5加密
    // Md5JMone(pw) {
    //   return (pw = this.mdjm(pw)
    //     .substr(0, this.mdjm(pw).length - 4)
    //     .substring(3));
    // },
    // mdjm(str) {
    //   return this.$md5(
    //     (str = str
    //       .trim()
    //       .substr(0, str.length - 1)
    //       .substr(1)
    //       .split("")
    //       .reverse()
    //       .join(""))
    //   );
    // },
    handleNodeClick(data) {
      console.log(data);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleClickq(row) {
      console.log(row);
      let arrr = [];
      let data = { OperID: row.Id };
      API_VILL_HQQX(data)
        .then((res) => {
          console.log(res.ResList);
          // this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())
          res.ResList.forEach((item) => {
            if (item.PreModelID != 0) {
              arrr.push(item.ModelName);
            }
          });
        })
        .catch((err) => {
          console.log(err);
        })
        .then((_) => {
          this.dialogVisibleq = true;
          this.accountid = row.Id;
        })
        .then((_) => {
          this.arrey = arrr;
        });
      console.log(this.arrey);
    },
    // 删除
    dele() {
      let data = this.multipleSelection.map((item) => {
        return {
          Id: item.Id,
          rolename: item.rolename,
          UsingFlag: item.UsingFlag,
          RoleId: item.RoleId,
          Password: item.Password,
          OperatorName: item.OperatorName,
          OperatorCode: item.OperatorCode,
          Note: item.Note,
          LastLogin: this.formatDate(item.LastLogin),
        };
      });
      if (data.length != 0) {
        API_OPERATOR_SC(data)
          .then((res) => {
            console.log(res);
            this.submit();
            this.$message.success("删除成功！");
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    handleClose() {
      this.$confirm("确认修改？")
        .then((_) => {
          // done();
          let data = {
            OperatorCode: this.inputcode,
            OperatorName: this.inputname,
            Note: this.inputremarks,
            UsingFlag: this.inputsign,
            RoleId: this.adcode,
            Password: this.addpw,
            Id: this.Id,
          };
          API_OPERATOR_XG(data)
            .then((res) => {
              console.log(res);
              this.submit();
              this.$message.success("修改成功！");
            })
            .catch((err) => {
              console.log(err);
            });
          this.clear();
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    cahnge(row) {
      this.Id = row.Id;
      this.inputcode = row.OperatorCode;
      this.inputname = row.OperatorName;
      // this.inputjurisdiction;
      // this.inputtime = row.;
      this.inputsign = row.UsingFlag.toString();
      this.inputremarks = row.Note;
      this.dialogVisible = true; // 修改模块显示隐藏
      this.adcode = row.RoleId;
      this.addpw = row.Password;
    },
    handleCloseadd() {
      console.log(1);
      this.$confirm("确认增加？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then((_) => {
          // console.log([].concat(...this.adcode));
          if (this.addpw == this.addpwo) {
            // this.addpw = this.Md5JMone(this.addpw);
            let data = {
              OperatorCode: this.addinputcode,
              OperatorName: this.addinputname,
              Note: this.addinputremarks,
              UsingFlag: this.addinputsign,
              RoleId: this.adcode,
              Password: this.addpw,
            };
            API_OPERATOR_NEW(data)
              .then((res) => {
                console.log(res);
                this.submit();
                this.$message.success("新增成功！");
                this.adddialogVisible = false;
                this.clear();
              })
              .catch((err) => {
                console.log(err);
              });
          } else {
            this.$message.warning("两次密码不一致！");
          }
        })
        .catch((_) => {
          this.$message.warning("失败");
        });
    },
    submit() {
      API_GET_OPERATOR_LIST()
        .then((res) => {
          console.log(res);
          this.tableData = res.ResList;
        })
        .catch((err) => {
          console.log(err);
        });
      API_GET_USER_LIST()
        .then((res) => {
          console.log(res);
          this.addicode = [];
          res.ResList.forEach((item) => {
            this.addicode.push({ value: item.Id, label: item.RoleName });
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    formatDate(time) {
      if (time != "") {
        if (time.toString().substr(6, 13) < 0) {
          return "";
        } else {
          return this.timeD(new Date(parseInt(time.toString().substr(6, 13))));
        }
      }
    },
    flatten(arr) {
      var res = [];
      arr.map((item) => {
        if (Array.isArray(item)) {
          res = res.concat(flatten(item));
        } else {
          res.push(item);
        }
      });
      return res;
    },
    timeD(date) {
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      var h = date.getHours();
      h = h < 10 ? "0" + h : h;
      var minute = date.getMinutes();
      minute = minute < 10 ? "0" + minute : minute;
      var second = date.getSeconds();
      second = second < 10 ? "0" + second : second;
      return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
    },
    clear() {
      this.addinputcode = "";
      this.addinputname = "";
      this.addinputjurisdiction = "";
      this.addinputtime = "";
      this.addinputsign = "";
      this.addinputremarks = "";
      this.inputcode = "";
      this.inputname = "";
      this.inputjurisdiction = "";
      this.inputtime = "";
      this.inputsign = "";
      this.inputremarks = "";
      this.adcode = "";
      this.addpw = "";
      this.addpwo = "";
      this.getList();
      this.dialogVisibleq = false;
      this.dialogVisible = false; // 修改模块显示隐藏
      this.adddialogVisible = false; // 新增模块显示隐藏
      this.arrey = [];
    },
    getList() {
      let ary = [];
      API_OPERATOR_GETCD()
        .then((res) => {
          // console.log(res);
          res.ResList.forEach((item) => {
            if (item.PreModelID == 0) {
              item.children = [];
              ary.push({
                id: item.ModelName,
                label: item.ModelName,
                children: [],
                qqq: item.Id,
                ModelCode: item.ModelCode,
              });
            }
          });
          ary = ary.sort(this.compare("ModelCode"));
          console.log(ary);
          res.ResList.forEach((item) => {
            ary.forEach((i, index) => {
              if (i.qqq == item.PreModelID) {
                i.children.push({
                  id: item.ModelName,
                  label: item.ModelName,
                  ModelCode: item.ModelCode,
                  ModelID: item.ModelID,
                });
                // i.children = i.children.sort(this.compare("ModelCode"));
                // console.log(i.children);
              }
            });
          });
          ary.forEach((item) => {
            item.children = item.children.sort(this.compare("ModelCode"));
          });
          console.log(ary);
          this.data = ary;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleCloseq() {
      // 获取所有选中数据的集合  console.log(this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()));
      // console.log(
      //   this.$refs.tree
      //     .getCheckedKeys()
      //     .concat(this.$refs.tree.getHalfCheckedKeys())
      // );
      this.$confirm("确认修改？")
        .then((_) => {
          let data = {
            lsname: this.$refs.tree
              .getCheckedKeys()
              .concat(this.$refs.tree.getHalfCheckedKeys())
              .toString(),
            accountid: this.accountid,
          };
          API_OPERATOR_SZQX(data)
            .then((res) => {
              console.log(res);
              this.$message.success("修改成功！");
              this.submit();
            })
            .catch((err) => {
              console.log(err);
            });
          // done();
          this.dialogVisibleq = false;
        })
        .catch((_) => {});
    },
    // 排序
    compare(property) {
      return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
      };
    },
  },
  created() {
    this.submit();
    this.getList();
  },
};
</script>

<style scoped lang="scss">
.operation {
  box-sizing: border-box;
  padding: 30px;
  // max-width: 1200px;
  .el-row {
    text-align: left;
    margin-bottom: 20px;
  }
}
</style>
